<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Conset_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
	
	function arrays($key){
    	$data = array(
    		'conset_subject_valid' 			=> array("1"=>"1", "2"=>"2","3"=>"3", "4"=>"4","5"=>"5", "6"=>"6","7"=>"7", "8"=>"8","9"=>"9", "10"=>"10", "11"=>"11", "12"=>"12"),
    		'conset_subject_file'=>array(	1=>'图片文件',2=>'文本文件',3=>'视频文件',4=>'音频文件',5=>'表格文件'),
    	);
    	
    	if($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return array();
    	}
    }
    
	function conset_use_add($data = array()){
		$this->db->insert('rsh_borrow_conset_use', $data);
		return $this->db->insert_id();
	}
	
	function conset_subject_add($data = array()){
		$this->mycache->delete('subject_title_list');
		$this->db->insert('rsh_borrow_conset_subject', $data);
		return $this->db->insert_id();
	}
	
	function conset_credit_add($data = array()){
		$this->mycache->delete('credit_title_list');
		$this->db->insert('rsh_borrow_conset_credit', $data);
		return $this->db->insert_id();
	}
	

	function conset_use_update($id, $data = array()){
		$this->mycache->delete('conset_use_obj_'.$id);
		$this->mycache->delete('use_title_list_all');
		$this->db->where('id', $id);
		$this->db->update('rsh_borrow_conset_use', $data);
		return $this->db->affected_rows();
	}
	
	function conset_subject_update($id, $data = array()){
		$this->mycache->delete('conset_subject_obj_'.$id);
		$this->mycache->delete('subject_title_list_all');		
		$this->db->where('id', $id);
		$this->db->update('rsh_borrow_conset_subject', $data);
		return $this->db->affected_rows();
	}
	
	function conset_credit_update($id, $data = array()){
		$this->mycache->delete('conset_credit_obj_'.$id);
		$this->mycache->delete('credit_title_list_all');
		$this->db->where('id', $id);
		$this->db->update('rsh_borrow_conset_credit', $data);
		return $this->db->affected_rows();
	}
	
	function get_conset_use_obj($id){
		$mkey = "conset_use_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,forder,content,add_time,is_del";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_borrow_conset_use');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	function get_conset_subject_obj($id){
		$mkey = "conset_subject_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,thumbnail,content,add_time,is_del,valid,fstatus";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_borrow_conset_subject');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	
	function get_conset_credit_obj($id){
		$mkey = "conset_credit_obj_".$id;
		$ret = $this->mycache->get_array($mkey);
		
		if($ret != false){
			return $ret;
		}
		$select = "id,title,thumbnail,content,add_time,is_del,rank";
		$this->db->select($select);
		$this->db->where(array('id'=>$id));
		$query = $this->db->get('rsh_borrow_conset_credit');
		$retsult = $query->row_array();
		$this->mycache->set_array($mkey,$retsult);
		return $retsult;
	}
	    	
	function get_conset_use_list($data){
		//封装查询内容
		$select = "id,title,forder,content,add_time,is_del";
		//封装条件
		$_where = " where is_del=0 ";
		
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_borrow_conset_use";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['conset_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_borrow_conset_use";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['conset_count'] = $query->row()->count;
		return $data;
	}
	
	function get_borrow_subject($subject){
		$select = "id,title,thumbnail,content,add_time,is_del,valid,fstatus";
		$_where = " where id in($subject) ";
		$sql_result = "select $select from rsh_borrow_conset_subject";
		$sql_result .= $_where;
		$query = $this->db->query($sql_result);
		return $query->result_array();
	}
	
	function get_conset_subject_list($data){
		//封装查询内容
		$select = "id,title,thumbnail,content,add_time,is_del,valid,fstatus";
		//封装条件
		$_where = " where is_del=0 ";
		
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_borrow_conset_subject";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['conset_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_borrow_conset_subject";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['conset_count'] = $query->row()->count;
		return $data;
	}
	
	function get_conset_credit_list($data){
		//封装查询内容
		$select = "id,title,thumbnail,content,add_time,is_del,rank";
		//封装条件
		$_where = " where is_del=0 ";
		
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and title like '%".$data['where']['title']."%'";
		}
		if(isset($data['where']['txt']) && $data['where']['txt'] != ''){
			$_where .= " and (title like '%".$data['where']['txt']."%' or content like '%".$data['where']['txt']."%')";
		}
		//排序
		$order  = " order by id desc ";
		//封装sql
		$page_size = $data['page_size'];
		$page_start = $data['page_start'];
		$sql_result = "select $select from rsh_borrow_conset_credit";
		$sql_result .= $_where;
		$sql_result .= $order;
		$sql_result .= " limit $page_start, $page_size";
		$query = $this->db->query($sql_result);
		$data['conset_list'] = $query->result_array();
		
		//总记录条数
		$sql_count = "select count(*) as count from rsh_borrow_conset_credit";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['conset_count'] = $query->row()->count;
		return $data;
	}
	
	
	// 获取所有的审核科目
	function get_subject_title_list(){
		$key = 'subject_title_list_all';
		$list = $this->mycache->get_array($key);
		if(!$list){
			$this->db->select('id,title');	
			$this->db->where(array('is_del'=>0));		
			$query = $this->db->get('rsh_borrow_conset_subject');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = $v['title'];
				}
				$this->mycache->set_array($key,$list);
			}
		}
		return $list;
	}
	

	// ****** 20141223  by liub 获取所有的安全等级
	function get_credit_title_list(){
		$key = 'credit_title_list_all';
		$list = $this->mycache->get_array($key);
		if(!$list){
			$this->db->select('id,title,thumbnail');
			$this->db->where(array('is_del'=>0));		
			$query = $this->db->get('rsh_borrow_conset_credit');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = array(0=>$v['title'],1=>$v['thumbnail']);
				}
				$this->mycache->set_array($key,$list);
			}
		}
		return $list;
	}
	
	// 获取所有的资金用途
	function get_use_title_list(){
		//$key = 'use_title_list_all';
		//$list = $this->mycache->get_array($key);
		//if(!$list){
			$this->db->select('id,title');
			$this->db->where(array('is_del'=>0));		
			$query = $this->db->get('rsh_borrow_conset_use');		
			$result = $query->result_array();
			$list = array();
			if($result){
				foreach($result as $v){
					$list[$v['id']] = $v['title'];
				}
				//$this->mycache->set_array($key,$list);
			}
		//}
		return $list;
	}
	
	
}